<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />

  <title>Class: TJPlurker::ThreadService</title>

  <link rel="stylesheet" href="../rdoc.css" type="text/css" media="screen" />

  <script src="../js/jquery.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
  <script src="../js/darkfish.js" type="text/javascript" charset="utf-8"></script>

</head>
<body id="top" class="class">

  <div id="metadata">
    <div id="home-metadata">
      <div id="home-section" class="section">
        <h3 class="section-header">
          <a href="../index.html">Home</a>
          <a href="../index.html#classes">Classes</a>
          <a href="../index.html#methods">Methods</a>
        </h3>
      </div>
    </div>

    <div id="file-metadata">
      <div id="file-list-section" class="section">
        <h3 class="section-header">In Files</h3>
        <div class="section-body">
          <ul>
          
            <li><a href="../lib/tjplurker_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
              class="thickbox" title="lib/tjplurker.rb">lib/tjplurker.rb</a></li>
          
          </ul>
        </div>
      </div>

      
    </div>

    <div id="class-metadata">
      
      <!-- Parent Class -->
      <div id="parent-class-section" class="section">
        <h3 class="section-header">Parent</h3>
        
        <p class="link"><a href="Service.html">TJPlurker::Service</a></p>
        
      </div>
      

      

      

      
      <!-- Method Quickref -->
      <div id="method-list-section" class="section">
        <h3 class="section-header">Methods</h3>
        <ul class="link-list">
          
          <li><a href="#method-c-new">::new</a></li>
          
          <li><a href="#method-i-final">#final</a></li>
          
          <li><a href="#method-i-serve">#serve</a></li>
          
        </ul>
      </div>
      

      
    </div>

    <div id="project-metadata">
      
      

      <div id="classindex-section" class="section project-section">
        <h3 class="section-header">Class/Module Index
          <span class="search-toggle"><img src="../images/find.png"
            height="16" width="16" alt="[+]"
            title="show/hide quicksearch" /></span></h3>
        <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
        <fieldset>
          <legend>Quicksearch</legend>
          <input type="text" name="quicksearch" value=""
            class="quicksearch-field" />
        </fieldset>
        </form>

        <ul class="link-list">
        
          <li><a href="../TJPlurker.html">TJPlurker</a></li>
        
          <li><a href="../TJPlurker/NotificationData.html">TJPlurker::NotificationData</a></li>
        
          <li><a href="../TJPlurker/Robot.html">TJPlurker::Robot</a></li>
        
          <li><a href="../TJPlurker/Service.html">TJPlurker::Service</a></li>
        
          <li><a href="../TJPlurker/ThreadService.html">TJPlurker::ThreadService</a></li>
        
        </ul>
        <div id="no-class-search-results" style="display: none;">No matching classes.</div>
      </div>

      
    </div>
  </div>

  <div id="documentation">
    <h1 class="class">TJPlurker::ThreadService</h1>

    <div id="description" class="description">
      
    </div><!-- description -->

    
    <div id="5Buntitled-5D" class="documentation-section">
      

      

      

      

      <!-- Methods -->
      
      <div id="public-class-method-details" class="method-section section">
        <h3 class="section-header">Public Class Methods</h3>

      
        <div id="new-method" class="method-detail ">
          <a name="method-c-new"></a>

          
          <div class="method-heading">
            <span class="method-name">new</span><span
              class="method-args">(name=self.class.to_s, lasting=60, refresh=1, &proc)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <table class="rdoc-list"><tr><td class="rdoc-term"><p><tt>lasting</tt></p></td>
<td>
<p>How long in second will a thread lives. If one plurk just be responsed,
then it will live another <tt>lasting</tt> time.</p>
</td></tr><tr><td class="rdoc-term"><p><tt>refresh</tt></p></td>
<td>
<p>Interval that checks if thread should terminate.</p>
</td></tr></table>
            

            
            <div class="method-source-code" id="new-source">
<pre>
<span class="ruby-comment"># File lib/tjplurker.rb, line 308</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span> <span class="ruby-identifier">name</span>=<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-identifier">lasting</span>=<span class="ruby-value">60</span>, <span class="ruby-identifier">refresh</span>=<span class="ruby-value">1</span>, &amp;<span class="ruby-identifier">proc</span>
    <span class="ruby-keyword">super</span> <span class="ruby-identifier">name</span>
    <span class="ruby-ivar">@lasting</span> = <span class="ruby-identifier">lasting</span>
    <span class="ruby-ivar">@refresh</span> = <span class="ruby-identifier">refresh</span>
    <span class="ruby-ivar">@plurk_id_hash</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
    <span class="ruby-ivar">@mutex</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- new-source -->
            
          </div>

          

          
        </div><!-- new-method -->

      
      </div><!-- public-class-method-details -->
    
      <div id="public-instance-method-details" class="method-section section">
        <h3 class="section-header">Public Instance Methods</h3>

      
        <div id="final-method" class="method-detail ">
          <a name="method-i-final"></a>

          
          <div class="method-heading">
            <span class="method-name">final</span><span
              class="method-args">(tjplurker, data)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Invoke after thread termination. You can use this to tell users this
service just terminate.</p>
            

            
            <div class="method-source-code" id="final-source">
<pre>
<span class="ruby-comment"># File lib/tjplurker.rb, line 318</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">final</span> <span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>

<span class="ruby-keyword">end</span></pre>
            </div><!-- final-source -->
            
          </div>

          

          
        </div><!-- final-method -->

      
        <div id="serve-method" class="method-detail ">
          <a name="method-i-serve"></a>

          
          <div class="method-heading">
            <span class="method-name">serve</span><span
              class="method-args">(tjplurker, data)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            
            

            
            <div class="method-source-code" id="serve-source">
<pre>
<span class="ruby-comment"># File lib/tjplurker.rb, line 322</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">serve</span> <span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">type</span><span class="ruby-operator">==</span><span class="ruby-string">'new_plurk'</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">decide</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        <span class="ruby-ivar">@plurk_id_hash</span>[<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>] = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">plurk_id</span><span class="ruby-operator">|</span>
            <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Create thread in plurk ##{plurk_id}&quot;</span>}
            <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:last_time</span>] = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
            <span class="ruby-identifier">loop</span>{
                <span class="ruby-identifier">sleep</span>(<span class="ruby-ivar">@refresh</span>)
                <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Plurk ##{plurk_id} remain #{Thread.current[:last_time]+@lasting-Time.now.to_i} seconds.&quot;</span>}
                <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
                <span class="ruby-keyword">if</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">-</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:last_time</span>] <span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@lasting</span>
                    <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
                    <span class="ruby-keyword">break</span>
                <span class="ruby-keyword">end</span>
                <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
            }
            <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Terminate thread in plurk ##{plurk_id}&quot;</span>}
            <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
            <span class="ruby-ivar">@plurk_id_hash</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">plurk_id</span>)
            <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
            <span class="ruby-identifier">final</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        }
    <span class="ruby-keyword">else</span>
        <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">type</span><span class="ruby-operator">==</span><span class="ruby-string">'new_response'</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@plurk_id_hash</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>)
            <span class="ruby-ivar">@plurk_id_hash</span>[<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>][<span class="ruby-value">:last_time</span>] = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
            <span class="ruby-identifier">action</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        <span class="ruby-keyword">end</span>
        <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
    <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- serve-source -->
            
          </div>

          

          
        </div><!-- serve-method -->

      
      </div><!-- public-instance-method-details -->
    
    </div><!-- 5Buntitled-5D -->
  

  </div><!-- documentation -->

  <div id="validator-badges">
    <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
    <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
      Rdoc Generator</a> 2</small>.</p>
  </div>

</body>
</html>

